/**
 * Created by chenzp on 18-8-17.
 */
layui.define(['jquery','layer','utl'],function (exports) {


    var $ = layui.$

    var utl = layui.utl


    var verify = {

        //非空验证
        required: {
            reg: function (value) {

                if (value == null || value == undefined) return false

                value = value.replace(/^\s*|\s*$/g, '')

                return /[\S]+/.test(value)
            }
            , msg: '必填项不能为空'
        }

        //手机号码验证
        , phone: {
            reg: /^1\d{10}$/
            , msg: '请输入正确的手机号'
        }

        //email验证
        , email: {
            reg: /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/
            , msg: '邮箱格式不正确'
        }

        //链接地址验证
        , url: {
            reg: /(^#)|(^http(s*):\/\/[^\s]+\.[^\s]+)/
            , msg: '链接格式不正确'
        }

        //数值验证
        , number: {
            reg: function (e) {
                if (!e || isNaN(e)) return false
                return true
            }
            , msg: '只能填写数字'
        }

        //日期验证
        , date: {
            reg: /^(\d{4})[-\/](\d{1}|0\d{1}|1[0-2])([-\/](\d{1}|0\d{1}|[1-2][0-9]|3[0-1]))*$/
            , msg: '日期格式不正确'
        }

        //简单身份证验证
        , identity: {
            reg: /(^\d{15}$)|(^\d{17}(x|X|\d)$)/
            , msg: '请输入正确的身份证号'
        }

        //汉字验证
        , character: {
            reg: /^[\u4e00-\u9fa5]{0,}$/
            , msg: '请输入汉字'
        }

        //字母验证

        , char: {
            reg: /^[a-zA-Z]*$/
            , msg: '请输入字母'
        }

        //整数验证
        , int: {
            reg: /^0$|^[1-9]\d*$/
            , msg: '请输入整数'
        }

        //数字和字母验证
        , charNum: {
            reg: /^0$|^[1-9]\d*$/
            , msg: '请输入字母和数字'
        }

        //高级身份证验证
        , identityReal: {
            reg: function (code) {
                var city = {
                    11: "北京",
                    12: "天津",
                    13: "河北",
                    14: "山西",
                    15: "内蒙古",
                    21: "辽宁",
                    22: "吉林",
                    23: "黑龙江 ",
                    31: "上海",
                    32: "江苏",
                    33: "浙江",
                    34: "安徽",
                    35: "福建",
                    36: "江西",
                    37: "山东",
                    41: "河南",
                    42: "湖北 ",
                    43: "湖南",
                    44: "广东",
                    45: "广西",
                    46: "海南",
                    50: "重庆",
                    51: "四川",
                    52: "贵州",
                    53: "云南",
                    54: "西藏 ",
                    61: "陕西",
                    62: "甘肃",
                    63: "青海",
                    64: "宁夏",
                    65: "新疆",
                    71: "台湾",
                    81: "香港",
                    82: "澳门",
                    91: "国外 "
                }
                var tip = ""

                if (!code || !/^\d{6}(18|19|20)?\d{2}(0[1-9]|1[12])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)$/i.test(code)) {
                    tip = "身份证号格式错误"
                    return false
                }

                else if (!city[code.substr(0, 2)]) {
                    tip = "地址编码错误"
                    return false
                } else {
                    //18位身份证需要验证最后一位校验位
                    if (code.length == 18) {
                        code = code.split('')
                        //∑(ai×Wi)(mod 11)
                        //加权因子
                        var factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
                        //校验位
                        var parity = [1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2]
                        var sum = 0
                        var ai = 0
                        var wi = 0
                        for (var i = 0; i < 17; i++) {
                            ai = code[i]
                            wi = factor[i]
                            sum += ai * wi
                        }
                        var last = parity[sum % 11]
                        if (parity[sum % 11] != code[17]) {
                            tip = "校验位错误"
                            return false
                        }
                    }
                }
                return true;
            }
            , msg: '请输入正确的身份证号'
        }

        //金钱验证
        , money: {
            reg: /(^(([1-9]+[0-9]*)|([0]{1}[.]{1})|([1-9]{1}[0-9]*[.]{1})){1}[0-9]*[0-9]{1}$)|(^[1-9]+[0-9]*$)/
            , msg: '数值格式不正确'
        }

        //商品价格
        , priceNumber: {
            reg: /(^(([1-9]+[0-9]*)|([0]{1}[.]{1})|([1-9]{1}[0-9]*[.]{1})){1}[0-9]*[1-9]{1}$)|(^[1-9]+[0-9]*$)/
            , msg: '格式不正确请输入大于零的值'
        }

        //商品价格小数位4位
        , pricePointNum: {
            reg: /^\d+(?:\.\d{1,4})?$/
            , msg: '格式不正确请输入至多4位小数位的值'
        }

        //商品价格可以等于零
        , price: {
            reg: /(^[0]*$)|(^0[.]{1}[0-9]*$)|(^[0-9]*$)|(^[0]*[1-9]+[0-9]*[.]{1}[0-9]+$)/
            , msg: '数值格式不正确'
        }

        //qq号
        , qq: {
            reg: /[1-9][0-9]{4,14}/
            , msg: '请输入正确的qq号'
        }
    }


    var formEle = {
        init:function (attr) {

        }
        ,mutli:function (target) {

        }
    }


    var mutli = {
        init:function () {
            var $b = $(`<b class="">+</b>`)
            this.$ele.after($b)

            var $this = this
            //绑定事件
            $b.on('',function () {
                var val = $this.$ele.val().trim()

                if(!$this.verifyVal(val)) {
                    return
                }

                $this.store  = $this.store ? $this.store:[]
                if($.inArray(val,$this.store) != -1) {
                    layer.msg('值不能一样')
                    return
                }

                var $btn = $(`<button type="button" class="layui-btn">${val}<b>x</b></button>`).data('data',val)

                $this.$ele.before($btn)

                $btn.find('b').on('click',function () {

                })

                $this.store.push(val)

            })
        }

        ,verifyVal:function (val) {

            var isOk = false
            if(this.opt.rep instanceof Array ){
                for(var i=0;i<this.opt.rep.length;i++){
                    var rep =this.opt.rep[i]
                    var verifyObj = verify[rep]
                    if (typeof  verifyObj.reg == 'function') {
                        isOk = verifyObj.reg(val)
                    } else {
                        isOk = verifyObj.reg.test(val)
                    }
                    if (!isOk) {
                        layer.msg(verifyObj.msg)
                        return isOk
                    }
                }
            }else{
                var verifyObj = verify[this.opt.reg]

                if (typeof  verifyObj.reg == 'function') {

                    isOk =  verifyObj.reg(val)

                    if(!isOk) {
                        layer.msg(verifyObj.msg)
                        return isOk
                    }

                } else {

                    isOk =   verifyObj.reg.test(val)

                    if(!isOk) {
                        layer.msg(verifyObj.msg)
                        return isOk
                    }
                }
            }

            return isOk
        }

        ,deleteTag:function (e) {
            var $target = $(e.currentTarget)
            for(var i=0;i<this.store.length;i++) {
                if (this.store[i] == $target.data('data')) {
                    this.store.splice(i,1)
                }
            }
            $target.remove()
        }
    }

    //扫描含有组件标签属性
    (function () {
        parseMain(['ly-mutli'])
    })()

    exports('fele',{})
})